package com.easytodo;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;

import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.StringRequestListener;
import com.bigkoo.pickerview.TimePickerView;
import com.easytodo.entity.Item;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AddActivity extends AppCompatActivity {

    TimePickerView pickerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
//        if (savedInstanceState == null) {
//            getSupportFragmentManager()
//                    .beginTransaction()
//                    .replace(R.id.settings, new SettingsFragment())
//                    .commit();
//        }
//        ActionBar actionBar = getSupportActionBar();
//        if (actionBar != null) {
//            actionBar.setDisplayHomeAsUpEnabled(true);
//        }

        initOther();
    }
    EditText title;
    EditText importance;
    EditText path;
    EditText need;
    EditText notice;
    Button submit;

    public void initOther(){
        initPickView();
        //初始化时间选择
        TextView time=findViewById(R.id.deadline);
        time.setText(getTimes(sd));

        //bind view
        title=findViewById(R.id.name);
        importance=findViewById(R.id.importance);
        path=findViewById(R.id.path);
        need=findViewById(R.id.need);
        notice=findViewById(R.id.notice);
        submit=findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
    }
    public void setTime(View v){

        if(pickerView!=null){
            pickerView.show(v);
        }
    }
    public void submit(){
        Log.v("MD","1????");
        Item item=new Item();
        item.setTitle(title.getText().toString());
        item.setDeathLine(sd.getTime());
        item.setImportance(importance.getText().toString());
        item.setPath(path.getText().toString());
        item.setNeedTime((long) (Integer.valueOf(need.getText().toString())*60*1000));
        item.setRemarks(notice.getText().toString());
        Log.v("MD",item.toString());
        Server.insertNewItem(item, new StringRequestListener() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getApplicationContext(),"添加成功",Toast.LENGTH_SHORT).show();
                Log.v("MD",response);
            }

            @Override
            public void onError(ANError anError) {

            }
        });
    }
    private void initPickView(){
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2029, 11, 28);
        pickerView=new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                TextView text=(TextView)v;
                text.setText(getTimes(date));
                sd=date;
            }
        })  //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .isCenterLabel(true)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(16)//字号
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(null)
                .build();
    }
    Date sd=new Date();
    private String getTimes(Date date) {//年月日时分秒格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}